#!/bin/bash

#if [ -e $1 ]; then
#    make debug
#else

port1=$(($1*1000+101))
port2=$(($1*1000+102))
port3=$(($1*1000+103))
port4=$(($1*1000+104))
port5=$(($1*1000+105))
port6=$(($1*1000+106))
port7=$(($1*1000+107))
port10=$(($1*1000+110))


    #compiling all source codes
    echo "compilando..."
    make all
    #removing unecessary .o
#    make clear
    rm out*

    #Give permission to execute
    chmod u+x *script1.sh *script.sh
	
    #running in background each node using ssh
    ./last_script.sh $port10 $port7 > outlast.txt &
	sleep 2
    echo "Node 17"
    ssh node17 "cd ~/T1/t1-sist-distrib/ && ./middlenode_script.sh 3 frontend $port7 $port6  > out017.txt ; exit" &
    sleep 2
    echo "Node 16"
    ssh node16 "cd ~/T1/t1-sist-distrib/ && ./middlenode_script.sh 3 frontend $port7 $port5 > out016.txt ; exit" &
    sleep 2

    echo "Node 15"
    ssh node15 "cd ~/T1/t1-sist-distrib/ && ./middlenode_script.sh 2 node17 $port6 $port4 > out015.txt ; exit" &
    sleep 2
    echo "Node 12"
    ssh node12 "cd ~/T1/t1-sist-distrib/ && ./middlenode_script.sh 2 node17 $port6 $port3 > out012.txt ; exit" &
    echo "Node 11"
    sleep 2
    ssh node11 "cd ~/T1/t1-sist-distrib/ && ./middlenode_script.sh 2 node16 $port5 $port2 > out011.txt ; exit" &
    sleep 2
    echo "Node 10"
    ssh node10 "cd ~/T1/t1-sist-distrib/ && ./middlenode_script.sh 2 node16 $port5 $port1 > out010.txt ; exit" &
    sleep 2

    echo "Node 1"
    ssh node1 "cd ~/T1/t1-sist-distrib/ && ./level1_script.sh node10 $port1 $port10 frontend > out001.txt ; exit" &
    sleep 2
    echo "Node 2"
    ssh node2 "cd ~/T1/t1-sist-distrib/ && ./level1_script.sh node10 $port1 $port10 frontend > out002.txt ; exit" &
    sleep 2
    echo "Node 3"
    ssh node3 "cd ~/T1/t1-sist-distrib/ && ./level1_script.sh node11 $port2 $port10 frontend > out003.txt ; exit" &
    sleep 2
    echo "Node 4"
    ssh node4 "cd ~/T1/t1-sist-distrib/ && ./level1_script.sh node11 $port2 $port10 frontend > out004.txt ; exit" &
    sleep 2
    echo "Node 5"
    ssh node5 "cd ~/T1/t1-sist-distrib/ && ./level1_script.sh node12 $port3 $port10 frontend > out005.txt ; exit" &
#node6 estava com problemas...
#   ssh node6 "cd ~/T1/t1-sist-distrib/ && ./level1_script.sh node12 $port3 $port10 frontend > out006.txt ; exit" $
    sleep 2
    echo "Node 7"
    ssh node7 "cd ~/T1/t1-sist-distrib/ && ./level1_script.sh node12 $port3 $port10 frontend > out007.txt ; exit" &
    sleep 2
    echo "Node 8"
    ssh node8 "cd ~/T1/t1-sist-distrib/ && ./level1_script.sh node15 $port4 $port10 frontend > out008.txt ; exit" &
    sleep 2
    echo "Node 9"
    ssh node9 "cd ~/T1/t1-sist-distrib/ && ./level1_script.sh node15 $port4 $port10 frontend > out009.txt ; exit" &

#fi
